package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.book.mapper.PermissionMapper;
import com.itany.book.pojo.Permission;
import com.itany.book.pojo.VOPermission;
import com.itany.book.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/26 16:01
 * Describe:
 * version:1.0
 */

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public PageInfo<Permission> findAll(Integer page, Integer rows, String name, String parentName) {

        PageHelper.startPage(page, rows);
        List<Permission> permissions = permissionMapper.selectByCondition(name,parentName);
        return new PageInfo<>(permissions);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> findAllParents() {
        return permissionMapper.selectAllParents();
    }

    @Override
    public List<VOPermission> findTree() {
        return permissionMapper.selectTree();
    }

    @Override
    public List<Permission> findMenuById(Integer id) {
        return permissionMapper.selectAllTree(id);
    }

    @Override
    public void modifyPermission(Permission p) {
        permissionMapper.updatePermission(p);
    }
}
